package com.bestseller.service.impl;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import org.apache.commons.lang.xwork.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.bestseller.bean.SysUserGroupSearch;
import com.bestseller.dao.IUserGroupDao;
import com.bestseller.pojo.SysUserGroup;
import com.bestseller.service.UserGroupService;
@Service(UserGroupService.SERVICE_NAME)
@Transactional(readOnly=true)
public class UserGroupServiceImpl implements UserGroupService {
	@Autowired
	private IUserGroupDao userGroupDao;
	
	@Override
	public void save(SysUserGroup userGroup) {
		userGroupDao.save(userGroup);
	}

	/*@Override
	public List<SysUserGroup> selectOfCondition(String name, String principal) {
		StringBuilder hql=new StringBuilder();
		List<String> params=new ArrayList<>();
		if(StringUtils.isNotBlank(name)){
			hql.append("and o.name like ? ");
			params.add("%"+name+"%");
		}
		if(StringUtils.isNotBlank(principal)){
			hql.append(" and o.principal = ? ");
			params.add(principal);
		}
		Object[] param=params.toArray();
		
		LinkedHashMap<String,String> orderby=new LinkedHashMap<>();
		orderby.put("o.id", "desc");
		
		return userGroupDao.selectOfCondition(hql.toString(),param,orderby);
	}
*/
	@Override
	public List<SysUserGroup> getList(SysUserGroupSearch search) {
		if(search==null){
			throw new RuntimeException("系统出现异常，请销后重试！！！");
		}
		StringBuilder hql=new StringBuilder();
		List<String> params=new ArrayList<>();
		if(StringUtils.isNotBlank(search.getName())){
			hql.append("and o.name like ? ");
			params.add("%"+search.getName()+"%");
		}
		Object[] param=params.toArray();
		LinkedHashMap<String,String> orderby=new LinkedHashMap<>();
		orderby.put("o.id", "desc");
		List<SysUserGroup> selectOfCondition = userGroupDao.selectOfCondition(hql.toString(),param,orderby);
		return selectOfCondition;
	}

}
